﻿using System;
using System.ServiceModel;
using System.Web.Security;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.Foundation.Contrib.Data.MySql;
using Omniscient.Foundation.Data;
using Omniscient.Foundation.Security;
using Omniscient.TimeTracker.Data;
using Omniscient.TimeTracker.Data.Adapters.MySql;
using Omniscient.TimeTracker.Server;

namespace Omniscient.TimeTracker.StandaloneServer
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting server...");

            ApplicationManager.Current.StartApplication();

            // Register IRoleProvider
            ApplicationManager.Current.ObjectContainer.Register<IRoleProvider>((IRoleProvider)Roles.Provider);

            IConnectivity connectivity;
            connectivity = ApplicationManager.Current.ServiceProvider.GetService<IConnectivity>();

            // Register entity adapters
            TimeEntryAdapter timeEntryAdapter;
            timeEntryAdapter = new TimeEntryAdapter(connectivity);
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<TimeEntry>>(timeEntryAdapter);

            ClientAdapter clientAdapter;
            clientAdapter = new ClientAdapter(connectivity);
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<Client>>(clientAdapter);

            ProjectAdapter projectAdapter;
            projectAdapter = new ProjectAdapter();
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<Project>>(projectAdapter);

            TaskAdapter taskAdapter;
            taskAdapter = new TaskAdapter();
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<Task>>(taskAdapter);

            CategoryAdapter categoryAdapter;
            categoryAdapter = new CategoryAdapter();
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<Category>>(categoryAdapter);

            ResourceAdapter resourceAdapter;
            resourceAdapter = new ResourceAdapter();
            ApplicationManager.Current.ObjectContainer.Register<IEntityAdapter<Resource>>(resourceAdapter);

            // Start service hosts/listeners
            ServiceHost timeEntryHost;
            timeEntryHost = new ServiceHost(typeof(TimeEntryAdapterService));
            timeEntryHost.Open();

            ServiceHost securityHost;
            securityHost = new ServiceHost(typeof(SecurityService));
            securityHost.Open();

            ServiceHost resourceHost;
            resourceHost = new ServiceHost(typeof(ResourceService));
            resourceHost.Open();

            ServiceHost taskHost;
            taskHost = new ServiceHost(typeof(TaskAdapterService));
            taskHost.Open();

            ServiceHost clientHost;
            clientHost = new ServiceHost(typeof(ClientAdapterService));
            clientHost.Open();

            ServiceHost projectHost;
            projectHost = new ServiceHost(typeof(ProjectAdapterService));
            projectHost.Open();

            ServiceHost categoryHost;
            categoryHost = new ServiceHost(typeof(CategoryAdapterService));
            categoryHost.Open();

            Console.WriteLine("Server started.");
            Console.ReadLine();

            timeEntryHost.Close();
            securityHost.Close();
            resourceHost.Close();
            taskHost.Close();
            clientHost.Close();
            projectHost.Close();
            categoryHost.Close();
        }
    }
}
